package com.geekaca.d09;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class TestTryWithResources {
    public static void main(String[] args) {
        /**
         * IO流的优雅关闭
         * try-with-resource(JDK7后新增)
         */
        try (
            //只有Cloasable的子类才能放在这个括号里,实现自动被调用
            InputStream fis = new FileInputStream("hello.txt");) {
            int len = -1;
            byte[] buffer = new byte[512];
            while ((len = fis.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, len));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
